import 'package:flutter/cupertino.dart';
import 'package:oktoast/oktoast.dart';
import 'package:rapid_kit/rapid_kit.dart';

import '../../repository/api.dart';
import '../../repository/datas/log_keys_data.dart';
import '../../sample/configure/constants.dart';
import '../../utils/LogUtils.dart';
import '../../utils/constants.dart';
import '../../utils/persistent_storage.dart';
import '../../utils/snack_tips.dart';
import '../../utils/sp_utils.dart';

/// 登录api类
class AuthViewModel with ChangeNotifier {
  // 实例
  RegisterInfo registerInfo = RegisterInfo();
  LoginInfo loginInfo = LoginInfo();

  Future<bool> _onAccessTokenAuth(String? accessToken) async {
    final authResp = await Authenticate.configure(accessToken ?? "");

    if (!authResp.success) {
      print("登录失败-身份有问题");
      return false;
    } else {
      LogUtils.w("token正常=${accessToken}");
      SharedPreferencesUtil.instance
          .setString(SampleConstants.storageKeyLoginToken, accessToken ?? "");
    }
    return true;
  }

  Future<bool?> register() async {
    dynamic callback = await Api.instance.register(
      accout: registerInfo.accout,
      username: registerInfo.username,
      password: registerInfo.password,
    );

    if (callback is bool) {
      return callback;
    } else {
      return true;
    }
  }

  Future<bool?> login() async {
    if (loginInfo.name != null && loginInfo.password != null) {
      LogUtils.w(loginInfo.name);
      LogUtils.w(loginInfo.password);

      LogKeysData? data = await Api.instance
          .login(name: loginInfo.name, password: loginInfo.password);

      // LogUtils.w("登录数据${data?.actualName}");
      // LogUtils.w("登录数据${data?.phone}");
      // LogUtils.w("登录数据${data?.token}");
      // LogUtils.w("登录数据${data?.tangeLoginInfo}");

      // 处理token
      await _onAccessTokenAuth(data?.tangeLoginInfo);

      if (data?.actualName != null && data?.tangeLoginInfo != null) {
        // print("===============账号-登录成功===============");
        SpUtils.saveString(Constants.SP_USER_NAME, data?.actualName ?? "");
        SpUtils.saveString(Constants.SP_USER_PHONE, data?.phone ?? "");
        SpUtils.saveString(Constants.SP_TG_TOKEN, data?.tangeLoginInfo ?? "");
        SpUtils.saveString(Constants.SP_ZHSL_TOKEN, data?.token ?? "");
        SpUtils.saveString(Constants.SP_USER_ID, "${data?.userId}" ?? "");
        return true;
      } else {
        showToast("登录异常");
        return false;
      }
    }
    showToast("输入不能为空");
    return false;
  }

  // 账号赋值方法
  void setLoginInfo({String? username, String? password}) {
    // LogUtils.w(username);
    // LogUtils.w(password);
    if (username != null) {
      loginInfo.name = username;
    }

    if (password != null) {
      loginInfo.password = password;
    }
  }

  void setRegisterInfo(
      {String? accout,
      String? username,
      String? password,
      String? rePassword}) {
      registerInfo.accout = accout;
      registerInfo.username = username;
      registerInfo.password = password;
      registerInfo.rePassword = rePassword;
  }
}

class RegisterInfo {
  String? accout;
  String? username;
  String? password;
  String? rePassword;
}

class LoginInfo {
  String? name = "";
  String? password = "";
}
